<?php
class OrderModule
{
	function index(){
		global $_G;
		if(!$_G['users']['shopid']){
			error('拒绝访问',url('home.php'));
		}
		$action = isset($_GET['a'])?$_GET['a']:null;
		$lang = lang::load('user');
		/*
		 * 对商家  确认订单(确认什么？)=>外送 =>等待用户收货=>完成订单
		 * 用户  下订单=>等待外送=>确认收货=>订单完成  用户超过1天未确认收货，默认完成
		 * 等待确认  送货中  等待收货 确认收货  完成
		 * 订单状态
		 * 0,1 等确认   商家确认订单 用户无操作
		 * 4 外送中， 商家/系统 确认订单订单后 ，用户：确认收货  商家：等待用户收货    
		 * 5 交易完成
		 * 
		 */
		
		$orderstatus = array(
			'0' => '等待确认',
			'1' => '打包中',
			'4' => '外送中',
			'5' => '结束',
			'9'	=> '交易取消',
		
		);
		switch($action) {
			case 'new':
				loadialog();
				$limit = 15;
				$caidans = $datas = $ids = array();
				$field = getGP('search_field','P');
				$where = '';
				if($field) {
					$where = " AND (o.txnid='".$field."' OR s.phone LIKE '%".$field."%') ";
				}
				$sql = 'SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.touid="'.$_G['users']['shopid'].'"'.$where;
				$query = $_G['db']->page_query($sql,$limit);
				$num = $_G['db']->result('SELECT count(o.txnid) FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.touid="'.$_G['users']['shopid'].'"'.$where);
				while($data = $_G['db']->fetch_array($query)) {
					$datas[] = $data ;
					$ids[] = $data['txnid'];
				}
				if(!empty($ids)){
				$query = $_G['db']->query('SELECT * FROM'.table('orders_product').'WHERE txnid IN('.implode(',', $ids).')');
				while($data = $_G['db']->fetch_array($query)){
					$caidans[$data['txnid']][] = $data;
				}
				}
				$pager = pager($num,$limit);
				break;
			case 'quick':

				break;
			case 'fee':
				
				break;
			case 'increase':
				$id = getGP('id','G','int');
				$txnid = getGP('txnid','G','int');
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$txnid.'" AND o.touid="'.$_G['users']['shopid'].'"');
				//权限检查
				if(!$id || !$txnid || !$orders['txnid']){
					$return['s'] = false;
					$return['m'] = '';
					print_r(json_encode($return));
					exit;
				}
				$products = $_G['db']->fetch('orders_product','*', array('txnid' =>$txnid,'pid'=>$id));
				$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
				
				if( 1 > $good['stock']){//不存在或者库存为0
					$return['s'] = false;
					$return['m'] = '';
				}elseif($good['pid']){
					if(!empty($products['pid'])){
						$_G['db']->update('orders_product',array('qty'=>$products['qty']+1),array('pid'=>$id,'txnid'=>$txnid));
						$_G['db']->update('orders',array('gross'=>$orders['gross'] + $products['price']),array('txnid'=>$txnid));
						$products['qty'] +=1;
					}
					$return['s'] = true;
					$return['q'] = $products['qty'];
					$return['p'] = $products['qty'] * $products['price'];
					$return['t'] = $orders['gross'] + $products['price'];
				}
				print_r(json_encode($return));
				exit;
				break;
			case 'decrease':
				$id = getGP('id','G','int');
				$txnid = getGP('txnid','G','int');
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$txnid.'" AND o.touid="'.$_G['users']['shopid'].'"');
				//权限检查
				if(!$id || !$txnid || !$orders['txnid']){
					$return['s'] = false;
					$return['m'] = '';
					print_r(json_encode($return));
					exit;
				}
				$products = $_G['db']->fetch('orders_product','*', array('txnid' =>$txnid,'pid'=>$id));
				$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
					
				if( !$good['pid']){//不存在
					$return['s'] = false;
					$return['m'] = '';
				}elseif($good['pid']){
					$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
					if($orders['gross'] - $products['price']<$shop['minprice']){
						$return['s'] = false;
						$return['m'] = '操作失败，原因：将导致订单总价少于最少起送价'.$shop['minprice'].'元';
					}else{
					
					
					if($products['qty']-1 >=0){
						if(!empty($products['pid'])){
							if($products['qty'] == 1){
								$_G['db']->query('DELETE FROM '.table('orders_product').'WHERE pid="'.$id.'" AND txnid="'.$txnid.'"');
							}else{
								$_G['db']->update('orders_product',array('qty'=>$products['qty']-1),array('pid'=>$id,'txnid'=>$txnid));
							}
							$_G['db']->update('orders',array('gross'=>$orders['gross'] - $products['price']),array('txnid'=>$txnid));
							$products['qty'] -=1;
						}
						$return['s'] = true;
						$return['q'] = $products['qty'];
						$return['p'] = $products['qty'] * $products['price'];
						$return['t'] = $orders['gross'] - $products['price'];
					}
					}
				}
				print_r(json_encode($return));
				exit;
				break;
			case 'clear':
				$id = getGP('id','G','int');
				$txnid = getGP('txnid','G','int');
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$txnid.'" AND o.touid="'.$_G['users']['shopid'].'"');
				//权限检查
				if(!$id || !$txnid || !$orders['txnid']){
					$return['s'] = false;
					$return['m'] = '';
					print_r(json_encode($return));
					exit;
				}
				$products = $_G['db']->fetch('orders_product','*', array('txnid' =>$txnid,'pid'=>$id));
				$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id));
				
				if( !$good['pid']){//不存在
					$return['s'] = false;
					$return['m'] = '';
				}elseif($good['pid']){
					$shop = $_G['db']->fetch('shops','minprice,minnum,extprice',array('shopid'=>$good['shopid']));
					if($orders['gross'] - $products['price']* $products['qty']<$shop['minprice']){
						$return['s'] = false;
						$return['m'] = '操作失败，原因：将导致订单总价少于最少起送价'.$shop['minprice'].'元';
					}else{	
						$_G['db']->query('DELETE FROM '.table('orders_product').'WHERE pid="'.$id.'" AND txnid="'.$txnid.'"');
						$_G['db']->update('orders',array('gross'=>$orders['gross'] - $products['price'] * $products['qty']),array('txnid'=>$txnid));
						
						$return['s'] = true;
						$return['q'] = 0;
						$return['p'] = 0;
						$return['t'] = $orders['gross'] - $products['price'] * $products['qty'];
					}	
				}
				print_r(json_encode($return));
				exit;
				break;
			case 'add':
				$id = getGP('id','G','int');
				$txnid = getGP('txnid','G','int');
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$txnid.'" AND o.touid="'.$_G['users']['shopid'].'"');
				//权限检查
				if(!$id || !$txnid || !$orders['txnid']){
					$return['s'] = false;
					$return['m'] = '';
					print_r(json_encode($return));
					exit;
				}
				$products = $_G['db']->fetch('orders_product','*', array('txnid' =>$txnid,'pid'=>$id));
				$good = $_G['db']->fetch('caidan','price,stock,shopid,pid,title,image',array('status'=>1,'pid'=>$id,'shopid'=>$_G['users']['shopid']));
				
				if( !$good['pid']){//不存在
					$return['s'] = false;
					$return['m'] = '';
				}elseif($good['pid']){
					if($products['pid']){
						$_G['db']->update('orders_product',array('qty'=>$products['qty']+1),array('pid'=>$id,'txnid'=>$txnid));
					}else{
						$insertdata = array('txnid'=>$txnid,'pid'=>$id,'title'=>$good['title'],'price'=>$good['price'],'qty'=>1);
						$_G['db']->insert('orders_product',$insertdata);
					}	
					$_G['db']->update('orders',array('gross'=>$orders['gross'] + $good['price'] ),array('txnid'=>$txnid));
					
					$return['s'] = true;
					$return['q'] = ($products['qty']+1);
					$return['p'] = ($products['qty']+1) * $good['price'];
					$return['up'] =  $good['price'];
					$return['h'] = $good['title'];
					$return['t'] = $orders['gross'] + $good['price'];
				}
				print_r(json_encode($return));
				exit;
				break;
			case 'cancel':
				$id = getGP('id','G','int');
				$return['s'] = false;
				$return['m'] = '';
				if(!$id){
					json($return);
				}
				$orders = $_G['db']->fetch('orders','txnid,status' ,array('txnid'=>$id,'touid'=>$_G['users']['shopid']));
				if(!$orders['txnid']){
					json($return);
				}
				$_G['db']->update('orders',array('status'=>9),array('txnid'=>$id));
				$return['s'] = true;
				$return['m'] = '操作成功';
				json($return);
				break;
			case 'status':
				$id = getGP('id','G','int');
				$return['s'] = false;
				$return['m'] = '';
				if(!$id){
					json($return);
				}
				$orders = $_G['db']->fetch('orders','txnid,status' ,array('txnid'=>$id,'touid'=>$_G['users']['shopid']));
				if(!$orders['txnid']){
					json($return);
				}
				switch($orders['status']){
					case '0':
						$_G['db']->update('orders',array('status'=>1),array('txnid'=>$id));
						$return['s'] = true;
						$return['m'] = '操作成功';
						json($return);
						break;
					case '1':
						$_G['db']->update('orders',array('status'=>4),array('txnid'=>$id));
						$return['s'] = true;
						$return['m'] = '操作成功';
						json($return);
						break;
					case '4':
						$_G['db']->update('orders',array('status'=>5),array('txnid'=>$id));
						$return['s'] = true;
						$return['m'] = '操作成功';
						json($return);
						break;	
				}
				break;
			case 'edit':
				$id = getGP('id','G','int');
				if(!$id){
					exit('access deny');
				}
				$orders = $_G['db']->fetch_one_array('SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.txnid="'.$id.'" AND o.touid="'.$_G['users']['shopid'].'"');
				if($orders['status']>=4){
					error('订单状态不允许再调整');
				}
				$products = $_G['db']->fetch_all('SELECT * FROM'.table('orders_product')."WHERE txnid ='".$id."'");
				break;
			case 'save':
				$id = getGP('id','P','int');
				if(!IS_POST || !$id){
					error('拒绝访问');
				}
				$phone = getGP('phone','POST');
				$addr = getGP('address','POST');
				$dtime = getGP('deliver_time','POST');
				$extra = getGP('note','POST');
				$shopid = getGP('shopid','POST');
				$shippfee = getGP('shippfee','POST');
				$catotal = getGP('catotal','POST');
				
				$txn = array(
						'shipping_cost' => $shippfee,
						'gross' => $catotal
				);
				$_G['db']->update("orders",$txn,array('txnid'=>$id));
				
				error('操作成功');
				
				
				break;
			default:
				loadialog();
				$limit = 15;
				$caidans = $datas = $ids = array();
				$field = getGP('search_field','P');
				$where = '';
				if($field) {
					$where = " AND (o.txnid='".$field."' OR s.phone LIKE '%".$field."%') ";
				}
				$sql = 'SELECT o.*,s.firstname,s.phone,s.street,s.shipping_fee FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.touid="'.$_G['users']['shopid'].'"'.$where;
				$query = $_G['db']->page_query($sql,$limit);
				$num = $_G['db']->result('SELECT count(o.txnid) FROM'.table('orders').'o LEFT JOIN'.table('orders_shipping').'s ON o.txnid=s.txnid WHERE o.touid="'.$_G['users']['shopid'].'"'.$where);
				while($data = $_G['db']->fetch_array($query)) {
					$datas[] = $data ;
					$ids[] = $data['txnid'];
				}
				if(!empty($ids)){
				$query = $_G['db']->query('SELECT * FROM'.table('orders_product').'WHERE txnid IN('.implode(',', $ids).')');
				while($data = $_G['db']->fetch_array($query)){
					$caidans[$data['txnid']][] = $data;
				}
				}
				$pager = pager($num,$limit);
				break;
		}
		if(getGP('ajax','G','int')){
			include template('uc/order_ajax');
		}else{
			include template('uc/order');
		}
	}
	
}	